package com.qk.management.controller;

import com.qk.common.util.Result;
import com.qk.common.util.AliyunOSSOperators;
import com.qk.management.anno.LogOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    /**
     * 文件上传
     */
    @Autowired
    private AliyunOSSOperators oss;

    @PostMapping("/upload")
    @LogOperation
    public Result upload(@RequestParam ("image")MultipartFile image)  {
        log.info("上传文件{}", image);
        try {
            Objects.requireNonNull(image, "上传文件不能为空");
            Objects.requireNonNull(image.getOriginalFilename(), "文件名不能为空");
            String objectName =UUID.randomUUID()+image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."));
            String url = oss.upload(image.getBytes(), objectName);
            return Result.success(url);
        } catch (Exception e) {
            log.error("文件上传失败", e.getMessage());
            return Result.error("上传失败");
        }
        }

    }
//    @PostMapping("/upload")
//    public String upload(String username, Integer age, MultipartFile file) throws Exception {
//        log.info("文件类型{}", file.getContentType());
//        log.info("文件大小{}", file.getSize());
//        log.info("文件名称{}", file.getName());
//        log.info("文件原始名称{}", file.getOriginalFilename());
//        file.transferTo(new File("D:/" + file.getOriginalFilename()));
//        return "上传成功";
//    }

